#coding=utf-8
#Boa:Dialog:DlgAccountDetail

import wx
import wx.grid

from product import *
from ctrlAider import *
from MyListCtrl import MyAccountDetailListCtrl

def create(parent):
    return DlgAccountDetail(parent)

[wxID_DLGACCOUNTDETAIL, wxID_DLGACCOUNTDETAILBTNLOAD, 
 wxID_DLGACCOUNTDETAILCBACCOUNT, wxID_DLGACCOUNTDETAILTACCOUNT, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgAccountDetail(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGACCOUNTDETAIL,
              name=u'DlgAccountDetail', parent=prnt, pos=wx.Point(303, 145),
              size=wx.Size(897, 567), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8d26\u6237\u8d44\u91d1\u660e\u7ec6')
        self.SetClientSize(wx.Size(889, 533))
        self.Center(wx.BOTH)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))

        self.cbAccount = wx.ComboBox(choices=[],
              id=wxID_DLGACCOUNTDETAILCBACCOUNT, name=u'cbAccount', parent=self,
              pos=wx.Point(120, 16), size=wx.Size(168, 27),
              style=wx.CB_READONLY, value=u'')
        self.cbAccount.SetLabel(u'')
        self.cbAccount.SetStringSelection(u'')
        self.cbAccount.Bind(wx.EVT_COMBOBOX, self.OnCbAccountCombobox,
              id=wxID_DLGACCOUNTDETAILCBACCOUNT)

        self.tAccount = wx.StaticText(id=wxID_DLGACCOUNTDETAILTACCOUNT,
              label=u'\u9009\u62e9\u8d26\u6237\uff1a', name=u'tAccount',
              parent=self, pos=wx.Point(24, 24), size=wx.Size(88, 16), style=0)

        self.btnLoad = wx.Button(id=wxID_DLGACCOUNTDETAILBTNLOAD,
              label=u'\u5bfc\u5165xls\u660e\u7ec6', name=u'btnLoad',
              parent=self, pos=wx.Point(304, 16), size=wx.Size(144, 32),
              style=0)
        self.btnLoad.Bind(wx.EVT_BUTTON, self.OnBtnLoadButton,
              id=wxID_DLGACCOUNTDETAILBTNLOAD)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()
        
    def myInit(self):
        self.cb = MyComboBoxDecorator(self.cbAccount, CDataAccount, CDataAccount.COL_BRIEFNAME)
        self.cb.refresh()
        self.curAccount = None
        
        self.lc = MyAccountDetailListCtrl(self)
        self.lc.SetSize(wx.Size(750, 300))
        self.lc.Move(wx.Point(30,60))
        
        self.OK = 0
        self.ERROR = -1
##        titles = [u'日期', u'收入', u'支出', u'余额', u'会款单号', u'备注']
##        [self.COL_DATE , self.COL_INCOME, self.COL_EXPEND, self.COL_REMAIN,
##        self.COL_BILLID, self.COL_MEMO] = [i for i in range(len(titles))]
##         
##        self.gridDetail.CreateGrid(100, len(titles))
##        for i in range(len(titles)):
##            self.gridDetail.SetColLabelValue(i, titles[i])
##        self.gridDetail.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)

    def loadExcel(self, file):
        import xlrd
        
        try:
            data = xlrd.open_workbook(file)
            table = data.sheet_by_index(0) # .sheets()[0] or .sheet_by_name(u'Sheet1')
            for i in range(table.nrows):
                date,income,expend,memo = table.row_values(i)
                date = date[:4] + '-' + date[4:6] + '-' + date[6:]
                print table.row_values(i)
    
                accountId = self.curAccount.getValue(CDataAccount.COL_ID)
                
                obj = CDataAccountDetail.factory()
                # 140711
                #obj.assignNewId()
                obj.setValue(CDataAccountDetail.COL_ACCOUNTID, accountId)
                obj.setValue(CDataAccountDetail.COL_DATE, date)
                if (income != '' and income != '/' and income != '\\'):
                    money = float(income)
                if (expend != '' and expend != '/' and expend != '\\'):
                    money = -float(expend)
                obj.setValue(CDataAccountDetail.COL_MONEY, money)
                #obj.setValue(CDataAccountDetail.COL_REMAIN, float(remain))
                #obj.setValue(CDataAccountDetail.COL_FUNDID, )
                obj.setValue(CDataAccountDetail.COL_MEMO, memo)
                obj.insert()
                
        except Exception,e:
            print e
            return self.ERROR
        
        return self.OK
     
    def refresh(self):
        accountId = self.curAccount.getValue(CDataAccount.COL_ID)
        self.lc.refresh({'accountId':accountId})
        
    def OnCbAccountCombobox(self, event):
        print self.cb.GetValue()
        self.curAccount = self.cb.getSelectObj()
        self.refresh()

    def OnBtnLoadButton(self, event):
        if(self.curAccount is None):
            dlg = wx.MessageDialog(self, u'请先选择资金账户',u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            return
            
        dlg = wx.FileDialog(self)
        if (dlg.ShowModal() == wx.ID_CANCEL):
            return
        
        file = dlg.GetPath()
        
        msg = u'请确认以下信息，以便导入数据：\n'
        msg += u'    导入账户：%s \n' %self.curAccount.getValue(self.curAccount.COL_BRIEFNAME)
        msg += u'    导入文件：%s' %file
        dlg = wx.MessageDialog(self, msg,u'提示',wx.YES_NO|wx.ICON_INFORMATION)
        if(dlg.ShowModal() == wx.ID_NO):
            return
    
        if(self.loadExcel(file) == self.OK):
            msg = u'导入文件成功'
        else:
            msg = u'导入文件失败，请检查文件格式是否正确'
            
        dlg = wx.MessageDialog(self, msg,u'提示',wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        
        self.refresh()
        
